const app = getApp();
var QQMap = require('../../utils/qqmap-wx-jssdk.js');//腾讯地图服务
var map = new QQMap({
  key: 'HFTBZ-QFORG-CURQ3-IFONA-PRY2F-EGBXH'
});
var nextPage = 0;
Page({
  data: {
    focus:true,
    searchSta:0,
    searchText:'',
    list: [],
    searchTip:[],
    history:[]
  },
  onLoad: function () {
    let that = this;
    wx.getStorage({
      key: 'histroy',
      success: function (res) {
        that.setData({
          history: res.data
        })
      },
    })
  },
  // 用户输入地址
  searchInput:function(e){
    let that = this;
    let searchSta = that.data.searchSta;
    if (e.detail.value.length>0){
      searchSta = 1;
    } else if (e.detail.value.length == 0){
      searchSta = 0;
    }
    that.setData({
      searchSta: searchSta,
      searchText: e.detail.value
    })
  },
  //返回首页
  back:function(){
    wx.navigateBack({
      delta:1
    })
  },
  // 搜索地址
  search:function(){
    let that = this;
    if (that.data.searchText.length > 0) {
      // 关键字提示输入
      map.getSuggestion({
        keyword: that.data.searchText,
        policy: 1,
        success: function (res) {
          console.log(res);
          if (res.data.length>0){
            that.setData({
              searchTip: res.data
            })
          }else{
            wx.showModal({
              title: '没搜到？',
              content: '一定是姿势不对，换个词试试',
              showCancel:false,
              confirmColor:'#333',
              success: function (res) {
                if (res.confirm) {
                 that.setData({
                   focus: true
                 })
                } 
              }
            })
          }
        }
      });
    }else{
      wx.showToast({
        title: '输入点什么吧',
        icon:'none'
      })
    }
  },
  //删除单个查询记录
  delHistoryOne:function(e){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除 ' + e.currentTarget.dataset.title +' 这个地址吗',
      confirmText: '删除',
      success: function (res) {
        if (res.confirm) {
          let title = e.currentTarget.dataset.title;
          let history = that.data.history;
          let result = [];
          for (var value of history) {
            if (value.title != title) {
              result.push(value);
            }
          }
          that.setData({
            history: result
          })
          wx.setStorage({
            key: 'histroy',
            data: that.data.history,
          })
        }
      }
    })
  },
  // 清空查询记录
  delHistory:function(){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定清空所有查询记录',
      confirmText:'清空',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            history: []
          })
          wx.setStorage({
            key: 'histroy',
            data: [],
          })
        }
      }
    })
    
  },
  // 当前位置进入天气详情
  zDetail:function(){
    let that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.reLaunch({
          url: '../index/index?lat=' + res.latitude + '&lng=' + res.longitude,
        })
      },
      fail:function(){
        wx.showModal({
          title: '定位失败',
          content: '没有位置授权无法发为您提供服务，请打开小程序授权',
          success: function (res) {
            if (res.confirm) {
              wx.openSetting({
                complete: function (data) {
                  console.log(data);
                  if (data.authSetting["scope.userLocation"]) {
                    wx.redirectTo({
                      url: '../index/index',
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  // 历史记录进入天气详情
  hDetail: function (e) {
    wx.reLaunch({
      url: '../index/index?lat=' + e.currentTarget.dataset.lat + '&lng=' + e.currentTarget.dataset.lng + '&title=' + e.currentTarget.dataset.title,
    })
  },
  // 搜索结果进入天气详情
  detail: function (e) {
    let that = this;
    let history = that.data.history;
    let searchResult = {'title':e.currentTarget.dataset.title, 'lat':e.currentTarget.dataset.lat, 'lng':e.currentTarget.dataset.lng};
    history.unshift(searchResult);
    that.setData({
      history: history
    })
    wx.setStorage({
      key: 'histroy',
      data: history,
    })
    wx.reLaunch({
      url: '../index/index?lat=' + e.currentTarget.dataset.lat + '&lng=' + e.currentTarget.dataset.lng + '&title=' + e.currentTarget.dataset.title,
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  }
})
